layui.use(['table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    /**
     * 出库订单表管理
     */
    var OutOrder = {
        tableId: "outOrderTable"
    };

    /**
     * 出库订单明细表管理
     */
    var OutOrderDetailed = {
        tableId: "outOrderDetailedTable"
    };

    /**
     * 初始化表格的列
     */
    OutOrder.initColumn = function () {
        return [[
            {type: 'checkbox', fixed: 'left'},
            {field: 'id', hide: true, title: '主键id'},
            {field: 'outOrderStatus', title: '单据状态'},
            {field: 'outCustomerName', title: '客户名称'},
            {field: 'storehouseName', title: '仓库名称'},
            {field: 'outOrderNumber', title: '出库订单号', style:'color: #2b85e4;font-weight: 600;'},
            {field: 'customerNumber', title: '客户单号', style:'color: #2b85e4;font-weight: 600;',width:150},
            {field: 'setMentiontime', title: '发货日期', style:'font-weight: 600;'},
            {field: 'outWay', title: '出库方式'},
            {field: 'transportWay', title: '运输方式'},
            {field: 'setUnit', title: '装货单位'},
            {field: 'setLocation', title: '装货地址'},
            {field: 'getUnit', title: '卸货单位', style:'font-weight: 600;'},
            {field: 'getLocation', title: '卸货地点'},
            {field: 'createUserName', title: '制单人员'},
            {field: 'createTime', title: '制单时间'},
            {field: 'submitterName', title: '提交人员'},
            {field: 'submitTime', title: '提交时间'},
            {align: 'center', toolbar: '#tableBar', title: '操作', fixed: 'right',width: 80}
        ]];
    };

    /**
     * 点击查询按钮
     */
    OutOrder.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        table.reload(OutOrder.tableId, {
            where: queryData, page: {curr: 1},
            done:function () {
                // 搜索按钮点击事件
                $('#btnSearch').click(function () {
                    OutOrder.search();
                });
                // 添加按钮点击事件
                $('#btnAdd').click(function () {
                    OutOrder.openAddDlg();
                });
                // 导出excel
                $('#btnExp').click(function () {
                    OutOrder.exportExcel();
                });
            }
        });
    };

    /**
     * 导出excel按钮
     */
    OutOrder.exportExcel = function () {
        var checkRows = table.checkStatus(OutOrder.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击处理
     *
     * @param data 点击按钮时候的行数据
     */
    OutOrder.openCannotDlg = function (data) {
        layer.open({
            type: 2,
            title: '出库订单审核',
            area: ['700px', '600px'],
            anim: 5,
            content: Feng.ctxPath + '/outOrder/toAudit?id=' + data.id,
            end: function () {
                table.reload(OutOrder.tableId);
                outOrder(null);
            }
        });
    };
    
    // 批量通过
    OutOrder.batchVia = function () {
    	var checkStatus = table.checkStatus('outOrderTable');
        data = checkStatus.data;
        if (data.length>0) {
            var ids = [];
            for (var i = 0; i < data.length; i++) {
            	ids.push(data[i].id);
			}
            layer.confirm('是否通过选中订单?', function(index){
            	layer.close(index);
            	var msg;
                $.ajax({
                	url:Feng.ctxPath + "/outOrder/batchVia?ids="+ids,
                	cache: false,
                	dataType:"json",
                	beforeSend:function(){
                    	//弹出加载中
                		msg = layer.msg('审核中。。。请耐心等待', {
	              			  icon: 16,
	              			  shade: [0.2,'#000'], //0.1透明度的白色背景
	              			  time: false
                		});
                	},
                	success:function(data){
                    	layer.close(msg); 
                		if (data.success) {
                        	Feng.success("审核成功!");
                        	OutOrder.search();
                            outOrder(null);
						} else {
							if (data.message !=null &&data.message!='') {
			    				var msg2 =  layer.open({
				  					  title: '提示' ,
				  					  content: data.message,
				  					  btn: ['确定'],
				  					  yes: function (){
				  						  	layer.close(msg2);
				                        	OutOrder.search();
				                            outOrder(null);
				  					  }
			    				});     
							} else {
	  		                    Feng.error("审核失败！");
	                        	OutOrder.search();
	                            outOrder(null);
							}
						}
                	},
                	error:function(data){
                    	layer.close(msg); 
                        Feng.error("审核失败！" + data.responseJSON.message)
                    	OutOrder.search();
                        outOrder(null);
                	}
                })
            }); 
		} else {
			layer.alert('请至少选中一条数据');
		}
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + OutOrder.tableId,
        url: Feng.ctxPath + '/outOrder/examineList',
        page: true,
        limit: 20,
        height: 380,
        cellMinWidth: 100,
        cols: OutOrder.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        OutOrder.search();
    });

    // 导出excel
    $('#btnExp').click(function () {
        OutOrder.exportExcel();
    });

    // 批量通过
    $('#btnVia').click(function () {
    	OutOrder.batchVia();
    });

    // 工具条点击事件
    table.on('tool(' + OutOrder.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'cannot') {
            OutOrder.openCannotDlg(data);
        }
    });

    /**
     * 初始化表格的列
     */
    OutOrderDetailed.initColumn = function () {
        return [[
            {field: 'id', hide: true, title: 'Primary Key'},
            {field: 'detailedOutNumber', title: 'Outbound Order No.',width: 120, style:'color: #2b85e4;font-weight: 600;'},
            {field: 'detailedFactory', title: 'Factory No.'},
            {field: 'detailedGoodsname', sort: true, title: 'Product Name', style:'background-color: #dbf1e1;font-weight: 600;',width: 270},
            {field: 'detailedGoodscode', sort: true, title: 'Product Code', totalRowText: 'Total：', style:'background-color: #dbf1e1;font-weight: 600;'},
            {field: 'detailedPutbalenum', sort: true, title: '大包数', totalRow: true, style:'background-color: #dbf1e1;font-weight: 600;'},
            {field: 'detailedNumber', sort: true, title: '数量', totalRow: true, style:'background-color: #dbf1e1;font-weight: 600;'},
            {field: 'detailedBatchnumber', sort: true, title: '批号', style:'background-color: #dbf1e1;font-weight: 600;'},
            {field: 'detailedOutWeight', sort: true, title: '重量', totalRow: true},
            {field: 'detailedOutVolume', sort: true, title: '体积', totalRow: true},
            {field: 'detailedPrice', title: '单价'},
            {field: 'detailedMoney', sort: true, title: '金额', totalRow: true},
            {field: 'detailedDate', sort: true, title: '生产日期'},
            {field: 'detailedPacking', title: '商品包装'},
            {field: 'detailedLargeclass', title: '商品大类'},
            {field: 'detailedGoodstype', title: '商品类别'},
        ]];
    };
    // 单击行事件
    table.on('row(' + OutOrder.tableId + ')', function (obj) {
        var data = obj.data;
		$('div[lay-id="' + OutOrder.tableId + '"]').find(".layui-table-body tr ").attr({ "style": "background:#FFFFFF" });//其他tr恢复原样
		$('div[lay-id="' + OutOrder.tableId + '"]').find(obj.tr.selector).attr({ "style": "background:#e6e6e6" });//改变当前tr颜色
        outOrder(data);
    });
    function outOrder(data){
        var outOrderNumber;
        if (data!=null) {
            $("#detailedOutNumber").val(data.outOrderNumber);
            outOrderNumber = data.outOrderNumber;
        } else {
            outOrderNumber = 'nodata';
        }
        // 渲染商品明细表格
        var tableResult = table.render({
            elem: '#' + OutOrderDetailed.tableId,
            url: Feng.ctxPath + '/outOrderDetailed/list?detailedOutNumber=' + outOrderNumber,
            page: true,
            limit: 20,
            height: 370,
            toolbar: '#toolbarfromDetailed',
            cellMinWidth: 100,
            totalRow: true ,//开启合计行
            cols: OutOrderDetailed.initColumn()
        });
    }

});
